﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightApplication1.Interfaces;
using SilverlightApplication1.Models;
using Path = System.IO.Path;

namespace SilverlightApplication1
{
    public partial class DetailsDialog : ChildWindow, IAnimated
    {
        private const String ExtTxt = ".txt";
        private const String DescritionDirectory = "RLS19G6";
        
        public DetailsDialog()
        {
            InitializeComponent();
                                  
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            CloseDialog();
        }

        private String GetDescritionFolderPath()
        {
            DirectoryInfo descritionDirectory = new DirectoryInfo(@Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                                                                                DescritionDirectory));
            if(!descritionDirectory.Exists)
            {
                descritionDirectory.Create();
            }
            return descritionDirectory.FullName;
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            String path = Path.Combine(GetDescritionFolderPath(),
                                       ((BlockModel)DataContext).Name + ExtTxt);
            if(File.Exists(path))
            {
                File.Delete(path);
            }

            using(TextWriter blockTextWriter = File.CreateText(path))
            {
                blockTextWriter.Write(textboxDescription.Text);                
            }
            ((BlockModel)DataContext).Description = textboxDescription.Text;    
        }

        public static DetailsDialog GetDialog()
        {
            return new DetailsDialog();
        }

        private void OpenDialog_Completed(Object sender, EventArgs e)
        {
            if (AnimationCompleted != null)
            {
                RunAnimation();
            }            
        }

        private void CloseDialog()
        {
            this.DialogResult = true;
        }

        public void RunAnimation()
        {
            Gallery.AnimationCompleted += CloseDialog;
            Gallery.AnimationCompleted += InvokeAnimationCompleted;
            Gallery.RunAnimation();
        }

        public event EventHandlers.AnimationCompletedHandler AnimationCompleted = null;

        public void InvokeAnimationCompleted()
        {
            if (AnimationCompleted != null)
            {
                AnimationCompleted();
            }
        }
        public void StopAnimation()
        {
            AnimationCompleted = null;
        }
    }
}

